package mypassForStu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import mypassForStu.common.R;
import mypassForStu.entity.Department;
import mypassForStu.entity.Teacher;
import mypassForStu.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    //根据年级、学院获取教师
    @PostMapping("/getByGrade")
    public R<List<Teacher>> getByGrade(@RequestBody Teacher teacher){
        LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teacher::getGrade, teacher.getGrade());
        queryWrapper.eq(Teacher::getDepartId, teacher.getDepartId());
        //筛选出辅导员
        queryWrapper.eq(Teacher::getT, 1);
        List<Teacher> teachers = teacherService.list(queryWrapper);
        return R.success(teachers);
    }

    //获取年级
    @PostMapping("/getGrade")
    public R<Set<Integer>> getGrade(@RequestBody Department department){

        LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teacher::getDepartId, department.getId());
        queryWrapper.eq(Teacher::getT, 1);
        List<Teacher> teachers = teacherService.list(queryWrapper);

        Set<Integer> grades = new HashSet<>();
        for(Teacher teacher: teachers){
            grades.add(teacher.getGrade());
        }
        if (grades.size()<1){
            return R.error("没有年级！");
        }
        return R.success(grades);
    }
}
